package com.ncloud.resman.mapper;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.resman.conf.EmptyStringIfNull;
import com.ncloud.resman.entity.DeviceCpe;
import com.ncloud.resman.entity.ExportDeviceCPE;

import org.apache.ibatis.annotations.*;

import java.sql.Date;
import java.util.List;

/**
 * Created by shiqiang on 2017/8/14.
 */
@Mapper
public interface DeviceCpeMapper {
    @Insert("insert into res_device_cpe(device_id, use_name, contact_phone, cust_name, put_date, out_date, cpu_model,cpu_num," +
            "memory_model,memory_capacity,disk_model,disk_capacity,sub_num," +
            "create_user,create_date,update_user,update_date)"
            + " values(#{deviceId},#{useName},#{contactPhone},#{custName},#{putDate},#{outDate},#{cpuModel},#{cpuNum},"
            + "#{memoryModel},#{memoryCapacity},#{diskModel},#{diskCapacity},#{subNum},"
            + "#{createUser},#{createDate},#{updateUser},#{updateDate})")
    int insertDeviceCpe(DeviceCpe deviceCpe);
    @Update("update res_device_cpe set use_name = #{useName},contact_phone=#{contactPhone},cust_name =#{custName},put_date =#{putDate}," +
            "out_date =#{outDate},cpu_model =#{cpuModel},cpu_num=#{cpuNum},memory_model=#{memoryModel},memory_capacity=#{memoryCapacity}," +
            "disk_model=#{diskModel},disk_capacity=#{diskCapacity},sub_num=#{subNum},create_user=#{createUser},create_date=#{createDate}," +
            "update_user=#{updateUser},update_date=#{updateDate} where device_id =#{deviceId}")
    int updateDeviceCpe(DeviceCpe deviceCpe);
    @Select(value = { "select a.* from res_device_cpe a where a.device_id = #{deviceId}" })
    @Results({
            @Result(column="device_id",property="deviceId"),@Result(column="use_name",property="useName"),
            @Result(column="contact_phone",property="contactPhone"),@Result(column="cust_name",property="custName"),
            @Result(column="put_date",property="putDate"),@Result(column="out_date",property="outDate"),
            @Result(column="cpu_model",property="cpuModel"),@Result(column="cpu_num",property="cpuNum"),
            @Result(column="memory_model",property="memoryModel"),@Result(column="memory_capacity",property="memoryCapacity"),
            @Result(column="disk_model",property="diskModel"),@Result(column="disk_capacity",property="diskCapacity"),
            @Result(column="sub_num",property="subNum"),@Result(column="create_user",property="createUser"),
            @Result(column="create_date",property="createDate"),@Result(column="update_user",property="updateUser"),
            @Result(column="update_date",property="updateDate")})
    DeviceCpe getDeviceCpeByDeviceId(Long deviceId);

    @Select(value = { "select a.* ,b.sn_num,f_get_dict('eqpt_type',b.eqpt_type) as eqptTypeName, b.eqpt_type , f_get_dict('eqpt_state',b.state) as eqptStateName , b.state as eqptState " +
            "  from res_device_base b, res_device_cpe a where a.device_id = b.device_id and  (#{0}='-1' or b.state = #{0}) " })
    @Results({ @Result(javaType = String.class,column="device_id",property="deviceId"),@Result(column="use_name",property="useName",typeHandler=EmptyStringIfNull.class),
            @Result(column="contact_phone",property="contactPhone",typeHandler=EmptyStringIfNull.class),@Result(column="cust_name",property="custName",typeHandler=EmptyStringIfNull.class),
            @Result(column="put_date",property="putDate",typeHandler=EmptyStringIfNull.class),@Result(column="out_date",property="outDate",typeHandler=EmptyStringIfNull.class),
            @Result(column="cpu_model",property="cpuModel"),@Result(column="cpu_num",property="cpuNum"),
            @Result(column="memory_model",property="memoryModel"),@Result(column="memory_capacity",property="memoryCapacity"),
            @Result(column="disk_model",property="diskModel"),@Result(column="disk_capacity",property="diskCapacity"),
            @Result(column="sub_num",property="subNum"),@Result(column="create_user",property="createUser"),
            @Result(column="create_date",property="createDate"),@Result(column="update_user",property="updateUser"),
            @Result(column="update_date",property="updateDate"),
            @Result(column = "eqpt_type", property = "eqptType"),
            @Result(column = "eqptTypeName", property = "eqptTypeName"),
            @Result(column = "eqptState", property = "eqptState"),
            @Result(column = "eqptStateName", property = "eqptStateName")})
    List<JSONObject> getDeviceCpeList(String state);

    /*删除*/
    @Delete(value = "delete from res_device_cpe WHERE device_id = #{0}")
    public int deleteDeviceCpe(Long deviceId);
    
  //导出CPE

    @Select(value = {"select a.device_id,b.sn_num,b.brand_name,b.model_id,b.model_name,a.cpu_model,a.cpu_num,a.memory_model,"
    		+ "a.memory_capacity,a.disk_model,a.disk_capacity,a.sub_num,a.put_date,a.out_date,a.use_name,a.contact_phone"
    		+ ",f_get_dict('eqpt_state',b.state) as state from res_device_cpe a,res_device_base b where a.device_id=b.device_id and IF(#{0} IS NULL,1=1"
    		+ ",b.sn_num=#{0}) "})
    @Results({@Result(column="device_id",property="deviceId"),
        @Result(column="sn_num",property="snNum"),@Result(column="brand_name",property="brandName"),
        @Result(column="model_name",property="modelName"),@Result(column="model_id",property="modelId"),@Result(column="cpu_model",property="cpuModel"),
        @Result(column="cpu_num",property="cpuNum"),@Result(column="memory_model",property="memoryModel"),
        @Result(column="memory_capacity",property="memoryCapacity"),@Result(column="disk_model",property="diskModel"),
        @Result(column="disk_capacity",property="diskCapacity"),@Result(column="sub_num",property="subNum"),
        @Result(column="put_date",property="putDate"),@Result(column="out_date",property="outDate"),
        @Result(column="use_name",property="useName"),@Result(column = "contact_phone", property = "contactPhone"),
        @Result(column = "state", property = "state")})
    public List<ExportDeviceCPE> exportCPEInfo(String snNum);
}
